function struct2TexArray(fid,struct,decimal)
% ========================================================================
% struct2TexArray(fid,struct,decimal)
%
%  Write the structure in struct into a tex equation array 
%  using fid as the file identifier for fprintf 
%  decimal is an optional integer controlling number of 
%  decimal places in sprintf; default is 5. 
%  If the number is smaller than 1e-5 then scientific notation will be used
%  If an entry is logical then it will be converted to true or false 
%  Alejandro Justiniano Sep 5 2012 
% ========================================================================
if nargin < 3 || isempty(decimal)
    decimal=5;
end
decimal=sprintf('%.0f',decimal);
structnames=fieldnames(struct);
fprintf(fid,'\\begin{eqnarray*} \n');
fprintf(fid,'\\begin{tabular}{rl} \n');

for ii=1:length(structnames);
    fldnm=char(structnames(ii));
    x=struct.(fldnm);
    str=strcat(fldnm,':');
    if islogical(x)==1 
        if x==false 
            x='false'; 
        else 
            x='true'; 
        end 
    end 
    switch isnumeric(x)
        %% Numeric
        case 1
            %% Single
            if length(x)==1
                if isinteger(x);
                    str=sprintf([str,'& %0.f'],x);
                elseif rem(1e5*x,1e5) < 1
                    str=sprintf([str,'& %.e'],x);
                else
                    str=sprintf([str,'& %.',decimal,'f'],x);
                end
            else
                %% Vector
                str=strcat(str,'&[');
                for jj=1:length(x)
                    if isnumeric(x(jj))
                        if isinteger(x(jj));
                            str=sprintf([str,'%0.f'],x(jj));
                        elseif rem(1e5*x(jj),1e5) < 1
                            str=sprintf([str,'%.e'],x(jj));
                        else
                            str=sprintf([str,' %.',decimal,'f'],x(jj));
                        end
                    end
                    if jj < length(x)
                        str=strcat(str,',');
                    end
                end
                str=strcat(str,']');
            end
        case 0
            str=sprintf([fldnm,'& %s'],char(x));
    end
    if ii < length(structnames);
        str=[str,' \\\\ \n'];
    else
        str=[str,' \n'];
    end
    fprintf(fid,str);
end
fprintf(fid,'\\end{tabular}\n');
fprintf(fid,'\\end{eqnarray*}\n');
%fprintf(fid,'\\newpage\n');
